﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SerialBrowserBridge.Devices
{
    public partial class ConsoleDevice : Form, IBlockDevice
    {
        public ConsoleDevice()
        {
            InitializeComponent();
        }

        #region -- blockdevice --
        public void WriteBlock(string data)
        {
            if (txtToConsole.InvokeRequired)
            {
                txtToConsole.Invoke(new Action<String>(WriteBlock), data);
                return;
            }
            txtToConsole.Text = data;
        }

        public event Action<string> ReadBlock;

        public bool Initialize()
        {
            return true;
        }

        public event Action<string, bool> Error; 
        #endregion

        private void ConsoleTextChanged(object sender, EventArgs e)
        {
            timeOutCounter = 3;
            tmrAutoSend.Enabled = true;
        }

        private int timeOutCounter = 0;

        private void AutoSendTick(object sender, EventArgs e)
        {
            if (timeOutCounter == 0)
            {
                if (ReadBlock != null)
                    ReadBlock(txtToBrowser.Text);

                lblStatus.Text = "Text wurde gesendet";
                tmrAutoSend.Enabled = false;
                return;
            }

            lblStatus.Text = String.Format("Noch {0} Sekunde{1} bis zum Senden", timeOutCounter, timeOutCounter == 1 ? "" : "n");

            timeOutCounter--;
        }



    }
}
